const helper = new Helper(this, true);
const prefix = helper.getVariable('environmentType');
const videoPlatform = helper.getVariable('videoPlatform');
// 基础服务
const basicRequest = BasicServices.getInstance(prefix);
// 流媒体服务
const streamRequest = StreamServices.getInstance(prefix);
// 应急服务
const emergencyRequest = EmergencyServices.getInstance(prefix);
// 物联 service
const iotServiceRequest = IotServiceServices.getInstance(prefix);

helper.log?.('页面初始化前')

// 处理设备列表树数据
function handleRows(data, rows, id) {
    return data.map(item => {
        if (item.id === id) return { ...item, isFetch: true, children: rows };
        if (item.children && item.children.length) {
            return { ...item, children: handleRows(item.children, rows, id) }
        }
        return item;
    })
}
function findParentNodes(tree, targetId, path = []) {
    for (const node of tree) {
        if (node.id === targetId) {
            return path;
        }
        if (node.children) {
            const result = findParentNodes(node.children, targetId, [...path, node.id]);
            if (result) {
                return result;
            }
        }
    }
    return null;
}
function extractNumber(input) {
    const regex = /\((\d+)\)/;
    const match = input.match(regex);
    if (!match || !match[1]) {
        return null;
    }
    return parseInt(match[1], 10);
}

function nodeHtmlText(id) {
    const node = $(`#${id}`).find('.tree-title')[0]
    const text = node.innerText.split('(')[0]
    const number = extractNumber(node.innerText) - 1
    node.innerText = text + '(' + number + ')'
}

// 设置设备列表树数据
function renderTree(rows, id, isDel) {
    const devices = helper.get('devices').widgetObj;
    const { dataSource } = devices._option;
    if (dataSource && +id !== -1) {
        devices._option.dataSource = handleRows(dataSource, rows, id);
    } else {
        devices._option.dataSource = handleRows(rows);
    }
    devices.render();
}

// 设备数据处理
function execEquipmentData(data) {
    const { equipmentName, equipmentCode, equipmentId, logicStatus, equipmentConfig } = data || {};
    const gbVideoConfig = equipmentConfig?.gbVideoConfig || {};
    let channelId = gbVideoConfig?.videoChannelId;
    let deviceId = gbVideoConfig?.sipUser;
    let stream_path;
    if (channelId && deviceId) stream_path = `${deviceId}/${channelId}`;
    /*const onlineImage = `<svg style="width: 20px;height: 20px;" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="200" height="200">
        <path d="M514.708995 382.871252c-59.597884 0-108.359788 48.761905-108.359789 108.359789s48.761905 108.359788 108.359789 108.359788 108.359788-48.761905 108.359788-108.359788-48.761905-108.359788-108.359788-108.359789zM469.559083 484.007055c-21.671958 0-39.731922-18.059965-39.731923-39.731923 0-21.671958 18.059965-39.731922 39.731923-39.731922s39.731922 18.059965 39.731922 39.731922c0 23.477954-18.059965 39.731922-39.731922 39.731923zM437.051146 700.726631h144.479718v90.299824h-144.479718z" fill="#C0EAFF" p-id="5381"></path>
    <path d="M511.097002 731.428571c-144.479718 0-261.869489-117.389771-261.869489-261.869488 0-41.537919 9.029982-81.269841 27.089947-117.389771 3.611993-9.029982 14.447972-12.641975 23.477954-7.223986s12.641975 14.447972 7.223986 23.477954c-16.253968 30.70194-23.477954 65.015873-23.477954 101.135803 0 124.613757 101.135802 225.749559 225.749559 225.749559S736.846561 594.17284 736.846561 469.559083s-101.135802-225.749559-225.749559-225.749559c-52.373898 0-102.941799 18.059965-142.673722 50.567901-7.223986 7.223986-19.865961 5.417989-25.28395-1.805996-7.223986-7.223986-5.417989-19.865961 1.805996-25.283951 46.955908-37.925926 106.553792-59.597884 166.151676-59.597884 144.479718 0 261.869489 117.389771 261.869488 261.869489s-117.389771 261.869489-261.869488 261.869488z" fill="#1F87DD" p-id="5382"></path><path d="M511.097002 617.650794c-72.239859 0-131.837743-59.597884-131.837743-131.837743s59.597884-131.837743 131.837743-131.837742 131.837743 59.597884 131.837742 131.837742-59.597884 131.837743-131.837742 131.837743z m0-225.749559c-52.373898 0-95.717813 43.343915-95.717813 95.717813s43.343915 95.717813 95.717813 95.717813 95.717813-43.343915 95.717813-95.717813-43.343915-95.717813-95.717813-95.717813z" fill="#1F87DD" p-id="5383"></path><path d="M511.097002 297.989418m-23.477954 0a23.477954 23.477954 0 1 0 46.955908 0 23.477954 23.477954 0 1 0-46.955908 0Z" fill="#1F87DD" p-id="5384"></path><path d="M583.336861 816.310406h-144.479718c-10.835979 0-18.059965-7.223986-18.059965-18.059965v-43.343915c0-10.835979 7.223986-18.059965 18.059965-18.059965s18.059965 7.223986 18.059965 18.059965v25.28395h108.359788v-74.045855c0-10.835979 7.223986-18.059965 18.059965-18.059965s18.059965 7.223986 18.059964 18.059965v92.10582c0 9.029982-7.223986 18.059965-18.059964 18.059965z" fill="#1F87DD" p-id="5385"></path><path d="M680.86067 816.310406H343.13933c-10.835979 0-18.059965-7.223986-18.059965-18.059965s7.223986-18.059965 18.059965-18.059965h337.72134c10.835979 0 18.059965 7.223986 18.059965 18.059965s-9.029982 18.059965-18.059965 18.059965z" fill="#1F87DD" p-id="5386"></path>
</svg>`*/
    const onlineImage = `<div style="width: 20px;min-width: 20px;height: 20px;margin-right: 5px;background: var(--equipment-online-status) no-repeat; background-size: 20px 20px;"></div>`
    const offlineImage = '<svg style="width: 24px;min-width: 24px;height: 24px;transform: translateX(-5px);"' +
        ' viewBox="0 0 824 824"' +
        ' xmlns="http://www.w3.org/2000/svg" width="200" height="200"><path d="M514.708995 382.871252c-59.597884 0-108.359788 48.761905-108.359789 108.359789s48.761905 108.359788 108.359789 108.359788 108.359788-48.761905 108.359788-108.359788-48.761905-108.359788-108.359788-108.359789zM469.559083 484.007055c-21.671958 0-39.731922-18.059965-39.731923-39.731923 0-21.671958 18.059965-39.731922 39.731923-39.731922s39.731922 18.059965 39.731922 39.731922c0 23.477954-18.059965 39.731922-39.731922 39.731923zM437.051146 700.726631h144.479718v90.299824h-144.479718z" fill="#8a8a8a" p-id="5381"></path><path d="M511.097002 731.428571c-144.479718 0-261.869489-117.389771-261.869489-261.869488 0-41.537919 9.029982-81.269841 27.089947-117.389771 3.611993-9.029982 14.447972-12.641975 23.477954-7.223986s12.641975 14.447972 7.223986 23.477954c-16.253968 30.70194-23.477954 65.015873-23.477954 101.135803 0 124.613757 101.135802 225.749559 225.749559 225.749559S736.846561 594.17284 736.846561 469.559083s-101.135802-225.749559-225.749559-225.749559c-52.373898 0-102.941799 18.059965-142.673722 50.567901-7.223986 7.223986-19.865961 5.417989-25.28395-1.805996-7.223986-7.223986-5.417989-19.865961 1.805996-25.283951 46.955908-37.925926 106.553792-59.597884 166.151676-59.597884 144.479718 0 261.869489 117.389771 261.869488 261.869489s-117.389771 261.869489-261.869488 261.869488z" fill="#8a8a8a" p-id="5382"></path><path d="M511.097002 617.650794c-72.239859 0-131.837743-59.597884-131.837743-131.837743s59.597884-131.837743 131.837743-131.837742 131.837743 59.597884 131.837742 131.837742-59.597884 131.837743-131.837742 131.837743z m0-225.749559c-52.373898 0-95.717813 43.343915-95.717813 95.717813s43.343915 95.717813 95.717813 95.717813 95.717813-43.343915 95.717813-95.717813-43.343915-95.717813-95.717813-95.717813z" fill="#8a8a8a" p-id="5383"></path><path d="M511.097002 297.989418m-23.477954 0a23.477954 23.477954 0 1 0 46.955908 0 23.477954 23.477954 0 1 0-46.955908 0Z" fill="#8a8a8a" p-id="5384"></path><path d="M583.336861 816.310406h-144.479718c-10.835979 0-18.059965-7.223986-18.059965-18.059965v-43.343915c0-10.835979 7.223986-18.059965 18.059965-18.059965s18.059965 7.223986 18.059965 18.059965v25.28395h108.359788v-74.045855c0-10.835979 7.223986-18.059965 18.059965-18.059965s18.059965 7.223986 18.059964 18.059965v92.10582c0 9.029982-7.223986 18.059965-18.059964 18.059965z" fill="#8a8a8a" p-id="5385"></path><path d="M680.86067 816.310406H343.13933c-10.835979 0-18.059965-7.223986-18.059965-18.059965s7.223986-18.059965 18.059965-18.059965h337.72134c10.835979 0 18.059965 7.223986 18.059965 18.059965s-9.029982 18.059965-18.059965 18.059965z" fill="#8a8a8a"></path></svg>'
    let online = 1;
    if (equipmentConfig?.equipmentUnifyConfig) {
        online = equipmentConfig.equipmentUnifyConfig.onlineStatus
    }
    /**
     * onlineStatus: { 0: 在线, 1: 离线, 2: 异常 }
     */
    return {
        ...data,
        title: equipmentName,
        stream_path,
        channelId,
        deviceId,
        onlineStatusImage: online === 0 ? onlineImage : offlineImage,
        isDraggable: !online,
        equipmentId: equipmentId || equipmentCode,
    }
}

// 区域数据处理
function handleAreaData(data, projectCode) {
    if (!data || !data.length) return [];
    return data.map(item => ({
        ...item,
        title: `${item.projectName || item.projectAreaName}(${item.equipmentCount})`,
        type: 'area',
        projectCode: projectCode || item.projectCode,
        children: [],
        isDraggable: false,
    }))
}

class RecordUtils {
    static forResult(result){
        return result.map(area => {
            area.title = area.projectAreaName || area.projectName
            area.type = 'area'
            area.isDraggable = false
            area.children = RecordUtils.forResult(area.projectLocationAreas || [])
            return area;
        })
    }
}

let __record__;

class RecordServices {
    params = {
        equipmentName: void 0,
    }
    // 获取单例
    static getInstance () {
        return __record__ || (__record__ = new RecordServices())
    }

    async queryEquipmentAreaTree () {
        const result = await basicRequest.getProjectAreaTree();
        const dataSource = RecordUtils.forResult(result.rows);
        renderTree(dataSource, '-1');
        helper.log('获取【项目子域】树', result)
        console.log('获取【项目子域】树', result, dataSource);
    }

    /**
     * 根据项目 Id，获取设备列表
     *
     * @param areaId
     * @return {Promise<void>}
     */
    async getEquipmentListByAreaId (areaId) {
        const { equipmentName, equipmentStatus } = this.params;
        // const loading = layer.load(0);
        const result = await basicRequest.getEquipmentListByAreaId({ areaId, equipmentName, equipmentStatus }) || [];
        // layer.close(loading);
        const dataSource = execEquipmentData(result) || []
        /*console.log(dataSource, 'dataSource222');
        const devices = helper.get('devices').widgetObj;
        devices.option_data = dataSource;
        devices.tree.expandSelected = new Set();
        devices.render();*/

        return result
    }
}


// 视频调阅页面全局属性/方法
window.RecordServices = RecordServices
window.RecordPage = { searchValue: "" };